#ifndef _STREAM
#define _STREAM

#include "..\Socket\Socket.h"
#include <iostream>
#include "..\Log\Log.h"

using namespace std;

#define BUFFER_SIZE 1024
#define PACKET_SIZE 1023

enum PacketType
{
    LOGIN,    
    NEWS,
    HEARTBEAT,
	EXIT
};

enum State
{
	REQUEST,
	RESPONSE,
	DONE
};

struct pHeader
{
	unsigned int rID;
	unsigned int sID;
    PacketType Type;
	State state;
	LPVOID object;    
};

struct packet
{
    pHeader Header;
    char msg[PACKET_SIZE - sizeof(pHeader) - 1];
	packet * next;
	packet * last;
};

class Stream : public Socket
{
private:
	Log m_log;
	int m_return;
	char message[1024];
public:
    Stream()
    {
		create();
    }

    virtual ~Stream() {}

    int send(packet * p)
    {
		m_return = Socket::send((char *)p, PACKET_SIZE + 1);
		sprintf(message, "Put %d onto the network.", PACKET_SIZE + 1);
		m_log.TRACE(5, message);
		//delete p;
		return m_return;
    }
	/*
    bool recv(River * r)
    {
        pHeader header;
        size_t offset = 0, bufferoffset = 0;
        int numbytes = 0;
        char buf[BUFFER_SIZE * 2], runoff[BUFFER_SIZE];
        while (1)
        {
            memset(buf, 0, BUFFER_SIZE + bufferoffset);
            if (bufferoffset > 0)
            {
                memcpy(buf, runoff, bufferoffset);
            }
            offset = 0;
            numbytes = Socket::recv(buf + bufferoffset,BUFFER_SIZE);
            if (numbytes < 1)
                return false;
            numbytes += bufferoffset;
            bufferoffset = 0;
            while (offset < numbytes)
            {
                packet * p = new packet();
                if (numbytes - offset > sizeof(pHeader))
                {
                    memcpy(&header, buf + offset, sizeof(pHeader));
                    offset += sizeof(pHeader);
                    if (numbytes - offset > header.size)
                    {
                        p->Header = header;
                        memcpy(p->msg, buf + offset, p->Header.size + 1);
                        offset += p->Header.size + 1;
                        process(p, r);
						//delete p;
                    }
                    else
                    {
                        bufferoffset = numbytes - offset + sizeof(pHeader);
                        memcpy(runoff, buf + offset - sizeof(pHeader), bufferoffset);
                        offset += numbytes;
                    }
                }
                else
                {
                    bufferoffset = numbytes - offset;
                    memcpy(runoff, buf + offset, bufferoffset);
                    offset += numbytes;
                }
            }
        }
    }
	*/
};

#endif